//
//  SYRefreshControl.swift
//  SingWeibo
//
//  Created by shiying on 15/12/23.
//  Copyright © 2015年 shiying. All rights reserved.
//

import UIKit

class SYRefreshControl: UIRefreshControl {
    
    private let arrowRotatingOffsetY: CGFloat = -60
    
    private var isUp = false

    
    override var frame: CGRect {
        didSet {
//            print("frame\(frame)")
            if frame.origin.y > 0 {
                return
            }
            
            /// 判断是否正在刷新
            if refreshing {
                refreshView.startLoging()
            }
            
            // 小于 - 60 isUp = true 向上刷新
            if frame.origin.y < arrowRotatingOffsetY && !isUp {
                
                isUp = !isUp
                refreshView.rotatingArrowView(isUp)
                
            } else if frame.origin.y > arrowRotatingOffsetY && isUp { // 如果大于就是向下 false
                
                isUp = !isUp
                refreshView.rotatingArrowView(isUp)
                
            }
        }
    }
    
    override func endRefreshing() {
        super.endRefreshing()
        refreshView.endRefreshing()
    }

    override init() {
        super.init()
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func prepareUI() {
        addSubview(refreshView)
        
        refreshView.ff_AlignInner(type: ff_AlignType.CenterCenter, referView: self, size: refreshView.bounds.size)
    }
    
    //MARK: - 懒加载
    lazy var refreshView: SYRefreshView = SYRefreshView.refreshView()

}


class SYRefreshView: UIView {

    @IBOutlet weak var arrowView: UIImageView!

    @IBOutlet weak var loadView: UIView!
    
    @IBOutlet weak var rotationView: UIImageView!
    
    /// 加载xib
    class func refreshView() ->SYRefreshView {
        let view = NSBundle.mainBundle().loadNibNamed("SYRefreshView", owner: nil, options: nil).last as! SYRefreshView
        return view
        
    }
    
    /// 旋转箭头
    func rotatingArrowView(isUp: Bool) {
//        print("箭头 \(isUp)")
        [UIView .animateWithDuration(0.5, animations: { () -> Void in
            self.arrowView.transform = isUp ? CGAffineTransformMakeRotation(CGFloat(M_PI - 0.02)) : CGAffineTransformIdentity;
        })]
    }
    
    
    func startLoging() {
        let animationKey = "animKey"
        if rotationView.layer.animationForKey(animationKey) != nil {
//            print("正在旋转...")
            return
        }
        
        loadView.hidden = true /// 隐藏下拉刷新
        
        let animation = CABasicAnimation(keyPath: "transform.rotation") /// 核心动画
        animation.toValue = 2 * M_PI  /// 旋转角度
        animation.repeatCount = MAXFLOAT  /// 旋转次数
        animation.duration = 0.5 /// 旋转时间
        animation.removedOnCompletion = false /// 不移除
        
        rotationView.layer.addAnimation(animation, forKey: animationKey)
    }
    
    /// 结束刷新
    func endRefreshing() {
        // 显示箭头View
        loadView.hidden = false
        
        //移除
        rotationView.layer.removeAllAnimations()
    }
    
    
}